import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created with Intellij IDEA.
 * Description;
 * User:墨琦
 * Data:2023-09-18
 * Time:17:57
 */
public class Java_2 {
    public static List<List<Integer>> threeSum2(int[] nums) {

            List<List<Integer>> list=new ArrayList<List<Integer>>();
            if(nums.length<3)
            {
                return list;
            }
            Arrays.sort(nums);
            for(int i=0;i<nums.length;i++)
            {
                if(nums[i]>0)
                {
                    break;
                }
                if(i>0&&nums[i]==nums[i-1])
                {
                    continue;
                }
                int left=i+1,right=nums.length-1,target=-nums[i];
                while(left<right)
                {
                    if(nums[left]+nums[right]>target)
                    {
                        right--;
                    }
                    else if(nums[left]+nums[right]<target)
                    {
                        left++;
                    }
                    else {
                        list.add(new ArrayList<Integer>(Arrays.asList(nums[i],nums[left],nums[right])));
                        left++;right--;
                        while(left<right&&nums[left]==nums[left-1]) left++;
                        while(left<right&& nums[right]==nums[right+1]) right--;
                    }
                }

            }
            return list;
        }
    

    public static List<List<Integer>> threeSum1(int[] nums) {
        List<List<Integer>> list=new ArrayList<List<Integer>>();
        if(nums.length<3)
        {
            return list;
        }
        int i=0;

        for(;i<nums.length-2;i++)
        {
            List<Integer> l2=new ArrayList<Integer>();
            int left=i+1;
            for(;left<nums.length-1;left++)
            {
               int right=nums.length-1;
                while(left<right)
                {
                    if(nums[left]+nums[right]+nums[i]==0)
                    {
                        l2.add(nums[left]);
                        l2.add(nums[right]);
                        l2.add(nums[i]);
                        list.add(l2);
                    }
                    right--;
                }
            }

        }
        return list;
    }
    public static void main(String[] args) {
        int[] array={-1,0,1,2,-1,-4};
        threeSum(array);
    }
}
